package array
{
	import flash.utils.ByteArray;
	import flash.utils.Endian;
	
	import data.MethodBody;
	
	import util.BytesUtil;
	import util.ExceptionUtil;
	import util.OpcodeUtil;
	import util.TraitUtil;

	public dynamic class MethodBodyArray extends Array
	{
		public function MethodBodyArray(...parameters)
		{
			super(parameters);
		}
		
		public function fill(bytes:ByteArray):void
		{
			var count:int = BytesUtil.readU30(bytes);
			if(count > 0){
				for(var i:int = 0; i < count; i++){
					var m:MethodBody = new MethodBody();
					this[i] = m;
					m.method = BytesUtil.readU30(bytes);
					m.max_stack = BytesUtil.readU30(bytes);
					m.local_count = BytesUtil.readU30(bytes);
					m.init_scope_depth = BytesUtil.readU30(bytes);
					m.max_scope_depth = BytesUtil.readU30(bytes);
					
					var code:ByteArray = new ByteArray();
					code.endian = Endian.LITTLE_ENDIAN;
					length = BytesUtil.readU30(bytes);
					bytes.readBytes(code, 0, length);
					var idx:int = 0;
					while(code.bytesAvailable){
						m.code[idx++] = OpcodeUtil.code(code);
					}
					trace("opcode>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
					OpcodeUtil.block(m.code);
					length = BytesUtil.readU30(bytes);
					for(idx = 0; idx < length; idx++){
						m.exceptions[i] = ExceptionUtil.exception(bytes);
					}
					
					length = BytesUtil.readU30(bytes);
					for(idx = 0; idx < length; idx++){
						m.traits[i] = TraitUtil.trait(bytes, App.cp);
					}
				}
			}
		}
	}
}